package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.constants.Constant;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.service.ClueRemarkService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class ClueRemarkController {

    @Autowired
    private ClueRemarkService clueRemarkService;

    @RequestMapping("/workbench/clue/saveClueRemark.do")
    @ResponseBody
    public Object saveClueRemark(ClueRemark clueRemark, HttpSession session){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        //封装数据
        clueRemark.setId(UUIDUtils.getUUID());
        clueRemark.setCreateTime(DateUtils.formateDataTime(new Date()));
        clueRemark.setCreateBy(user.getId());
        clueRemark.setEditFlag(Constant.REMARK_EDIT_NO_EDITED);

        try {
            //调用Service
            int result = clueRemarkService.saveClueRemark(clueRemark);

            if (result > 0) {
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
                returnObject.setRetData(clueRemark);
            } else {
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙请稍后再试！");
        }
        return returnObject;
    }

    @RequestMapping("workbench/clue/removeClueRemark.do")
    @ResponseBody
    public Object removeClueRemark(String id){
        ReturnObject returnObject = new ReturnObject();
        try {
            int result = clueRemarkService.deleteClueRemarkById(id);
            if(result>0){
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后再试...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后再试...");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/saveUpdateClueRemark.do")
    @ResponseBody
    public Object saveUpdateClueRemark(ClueRemark clueRemark,HttpSession session){
        User user =(User) session.getAttribute(Constant.SESSION_USER);
        //封装参数
        clueRemark.setEditBy(user.getId());//修改者
        clueRemark.setEditTime(DateUtils.formateDataTime(new Date()));
        clueRemark.setEditFlag(Constant.REMARK_EDIT_YES_EDITED);

        ReturnObject returnObject = new ReturnObject();

        //调用Service
        try {
            int result = clueRemarkService.updateClueRemark(clueRemark);
            if(result>0){
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
                returnObject.setRetData(clueRemark);
            }else{
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后再试...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后再试...");
        }
        return returnObject;
    }
}
